package com.manli.wechat.utils;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.manli.common.utils.HttpUtil;
import com.manli.wechat.pojo.WechatPayCashRedBao;
import com.manli.wechat.utils.wxpay.WxPayPojoV2;
import com.manli.wechat.utils.wxpay.config.MyWxPayConfig;
import com.manli.wechat.utils.wxpay.sdk.WXPay;

/**
 * 微信支付
 * @author Shensg
 *
 * 2020年4月9日
 */
public class WechatPay {

	public static final String manliCaPath = "D:\\Users\\Administrator\\Downloads\\WXCertUtil\\cert\\1583579551_20200409_cert\\apiclient_cert.p12";

	/*
	 * 现金红包
	 */
	public static String sendCASHREDBAO(WechatPayCashRedBao cashRedBao) throws Exception {
		String caPwd = cashRedBao.getMch_id();//商户号
		return HttpUtil.httpsPostByCA(WeChatUrl.CASH_RED_BAO ,cashRedBao.getXmlInfo(),HttpUtil.JSON,caPwd,manliCaPath);
	}

	/**
	 * 裂变红包
	 * @param cashRedBao
	 * @return
	 * @throws Exception
	 */
	public static String sendCASHSHAREREDBAO(WechatPayCashRedBao cashRedBao) throws Exception {
		String caPwd = cashRedBao.getMch_id();//商户号
		return HttpUtil.httpsPostByCA(WeChatUrl.CASH_SHARE_RED_BAO ,cashRedBao.getXmlInfo(),HttpUtil.JSON,caPwd,manliCaPath);
	}
	/**
	 * 统一下单
	 * @throws Exception 
	 */
	public static Map<String, String> preOrder(MyWxPayConfig config , Map<String, String> data) throws Exception {
		WXPay wxpay = new WXPay(config);
		try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
		return null;
	}
	/**
	 * 订单查询
	 * @param
	 * @throws Exception
	 */
	public static Map<String, String> orderQuery(MyWxPayConfig config , Map<String, String> data) throws Exception {
		WXPay wxpay = new WXPay(config);
		try {
            Map<String, String> resp = wxpay.orderQuery(data);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
		return null;
	}
	/**
	 * 退款查询
	 * @param config
	 * @param data
	 * @return
	 * @throws Exception 
	 */
	public static Map<String, String> refundQuery(MyWxPayConfig config , Map<String, String> data) throws Exception {
		WXPay wxpay = new WXPay(config);
		try {
            Map<String, String> resp = wxpay.refundQuery(data);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
		return null;
	}
	/**
	 *  下载对账单
	 * @param config
	 * @param data
	 * @return
	 * @throws Exception 
	 */
	public static Map<String, String> downloadBill(MyWxPayConfig config , Map<String, String> data) throws Exception {
		WXPay wxpay = new WXPay(config);
		try {
            Map<String, String> resp = wxpay.downloadBill(data);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
		return null;
	}


	/**
	 * 调用案例
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String certPath = "D:\\weixinpayca\\WXCertUtil\\cert\\1601420795_20200806_cert\\apiclient_cert.p12";
		
		//String appID = "wx3745551a988274db";
		String appID = "wx6f56d14c68184b2f";//漫狸科技（温州）
		String mchID = "1601420795";
		String key = "manli111111111111111111111111111";
		MyWxPayConfig config = new MyWxPayConfig(certPath, appID, mchID, key);
		
        WXPay wxpay = new WXPay(config);
        /**s
         * 	下载对账单
         */
        /*Map<String, String> data = new HashMap<String, String>();
        data.put("bill_date", "20200403");
        data.put("bill_type", "ALL");

        try {
            Map<String, String> resp = wxpay.downloadBill(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        /**
         * 	退款查询
         */
        /*Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", "manli202012");

        try {
            Map<String, String> resp = wxpay.refundQuery(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        /**
         * 	订单查询
         */
        /*Map<String, String> data = new HashMap<String, String>();
		data.put("out_trade_no", "manli290011");
        data.put("sub_mch_id", "1601704787");

        try {
            Map<String, String> resp = wxpay.orderQuery(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
		/**
		 * 统一下单
		 */

		WxPayPojoV2 wxPayPojoV2 = new WxPayPojoV2();
		wxPayPojoV2.setAppid("wx6f56d14c68184b2f");
		wxPayPojoV2.setMch_id("1601420795");
		wxPayPojoV2.setSub_mch_id("1601704787");
		wxPayPojoV2.setBody("漫狸充值中心");
		wxPayPojoV2.setAttach("attach");
		wxPayPojoV2.setOut_trade_no("manli090803");
		wxPayPojoV2.setTotal_fee("600");
		wxPayPojoV2.setSpbill_create_ip("114.114.114.114");
		wxPayPojoV2.setNotify_url("http://manli.mynatapp.cc/manli/wechatpay/orderPayNotifyo123");
		wxPayPojoV2.setTrade_type("JSAPI");
		wxPayPojoV2.setOpenid("oJRQnwvbiRCQGmw6xzMsBCme2ILg");
		wxPayPojoV2.setGoods_tag("20200908,20200908002");
		wxPayPojoV2.setProduct_id("12");
		Map map = JSON.parseObject(JSON.toJSONString(wxPayPojoV2), Map.class);
		Map<String, String> resp = wxpay.unifiedOrder(map);
		System.out.println(resp);
		/**
		 * 退款
		 */
		/*wxPayPojoV2 = new WxPayPojoV2();
		wxPayPojoV2.setAppid("wx6f56d14c68184b2f");
		wxPayPojoV2.setMch_id("1601420795");
		wxPayPojoV2.setSub_mch_id("1601704787");
		wxPayPojoV2.setOut_trade_no("manli29001");
		wxPayPojoV2.setTotal_fee("5");
		wxPayPojoV2.setOut_refund_no("manli290011");
		wxPayPojoV2.setRefund_fee("5");
		wxPayPojoV2.setRefund_desc("售完");
		wxPayPojoV2.setNotify_url("http://manli.mynatapp.cc/manli/wechatpay/orderRefuseNotify");

		Map map = JSON.parseObject(JSON.toJSONString(wxPayPojoV2), Map.class);
        try {
			Map<String, String> resp = wxpay.refund(map);
			System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
		/**
		 * 现金红包
		 */
		/*WechatPayCashRedBao cashRedBao = new WechatPayCashRedBao();
		cashRedBao.setNonce_str("123");
		cashRedBao.setMch_billno("manliOrderNo03");
		cashRedBao.setMch_id("1583579551");
		cashRedBao.setWxappid("wx3745551a988274db");
		cashRedBao.setSend_name("漫狸现金红包");
		cashRedBao.setRe_openid("oXzMAwMhnYDAcX_m9ZDAxSCjsS4c");
		cashRedBao.setTotal_amount(100);分
		cashRedBao.setScene_id("PRODUCT_1");
		cashRedBao.setTotal_num(1);
		cashRedBao.setWishing("红包祝福语");
		cashRedBao.setClient_ip("115.221.128.230");
		cashRedBao.setAct_name("活动名称");
		cashRedBao.setRemark("猜越多得越多，快来抢！");
		Map<String, String> map = BeanUtils.describe(cashRedBao);
		String assiiStr = WeChatSignUtils.getASCII(map,Arrays.asList(new String[] {"class","xmlInfo"}));
		System.out.println("@@@"+assiiStr);
		cashRedBao.setSign(WeChatSignUtils.getSign(assiiStr,"manli111111111111111111111111111"));
		System.out.println(WechatPay.sendCASHREDBAO(cashRedBao));;*/

		/**
		 * 裂变红包
		 */
		/*WechatPayCashRedBao cashRedBao = new WechatPayCashRedBao();
		cashRedBao.setNonce_str("123");
		cashRedBao.setMch_billno("LBmanliOrderNo03");
		cashRedBao.setMch_id("1583579551");
		cashRedBao.setWxappid("wx3745551a988274db");
		cashRedBao.setSend_name("漫狸现金红包");
		cashRedBao.setRe_openid("oXzMAwMhnYDAcX_m9ZDAxSCjsS4c");
		cashRedBao.setTotal_amount(300);分
		cashRedBao.setScene_id("PRODUCT_1");
		cashRedBao.setTotal_num(3);
		cashRedBao.setWishing("红包祝福语");
		cashRedBao.setClient_ip("115.221.128.230");
		cashRedBao.setAct_name("活动名称");
		cashRedBao.setRemark("猜越多得越多，快来抢！");
		cashRedBao.setAmt_type("ALL_RAND");
		Map<String, String> map = BeanUtils.describe(cashRedBao);
		String assiiStr = WeChatSignUtils.getASCII(map,Arrays.asList(new String[] {"class","xmlInfo"}));
		System.out.println("@@@"+assiiStr);
		cashRedBao.setSign(WeChatSignUtils.getSign(assiiStr,"manli111111111111111111111111111"));
		System.out.println(WechatPay.sendCASHSHAREREDBAO(cashRedBao));;*/
		
		
	}
}
